package main

import (
	"bytes"
	"fmt"
	"io"
	"log"
	"os"
	"spider/common"
	"spider/fetcher"
	"spider/itemSaver"
	"spider/models"
	"spider/models/meituan/meituanMeishiPc"
)



func main() {
	err := common.InitConfig("./conf.json")
	if err != nil {
		panic(err)
	}
	models.Connect()
	var lists []meituanMeishiPc.TableMeiTuanLicense
	orm := models.G_ormdb
	id := 0
	header := common.CommonHeader

	for {
		res := orm.Table("meituan_license").Where("id>?", id).Limit(10).Find(&lists).Order("id asc")
		if res.Error != nil {
			panic(res.Error)
		}
		fmt.Println("id===",id)
		num := len(lists)
		if num < 1 {
			break
		}

		id = lists[num-1].Id

		for _, item := range lists {
			poiId := item.PoiId
			BusinessLicenceImgUrl := item.BusinessLicenceImgUrl
			RestaurantLicenceImgUrl := item.RestaurantLicenceImgUrl
			path0, err := common.BuildPathFile(poiId, 0)

			oss_path0:=fmt.Sprintf("test/meituan_licence/%d/%d.jpg",poiId,0);

			ossUrl0:=fmt.Sprintf("https://img2.lepucdn.cn/test/meituan_licence/%d/%d.jpg@!original.jpg",poiId,0);



			if err != nil || len(BusinessLicenceImgUrl) < 1 {
				fmt.Printf("poiId=%d,err=%s\n",poiId, err)
			} else {
				//不存在去下载
				if !common.Exists(path0) {
					body, err := fetcher.FetchGet(BusinessLicenceImgUrl, header, "")
					if err != nil {
						fmt.Printf("poiId=%d,fecth_err=%s\n", poiId,err)
					} else {
						out, err := os.Create(path0)
						if err != nil {
							log.Printf("poiId=%d,Create_err=%s\n",poiId, err)
						} else {
							wlen, err := io.Copy(out, bytes.NewReader(body))
							if err != nil {
								log.Printf("poiId=%d,copy_err=%s\n", poiId,err)
							} else {
								fmt.Printf("poiId=%d,writeSuccess===========%d\n", poiId, wlen)
							}

						}

					}
				}else{
					//存在就上传到oss

					err=itemSaver.OssUpload(path0,oss_path0);
					if err!=nil{
						fmt.Println(err)
					}else{

						orm.Model(&meituanMeishiPc.TableMeiTuanLicense{}).Where("poiId=?",poiId).
							Update("ossBusinessLicenceImgUrl", ossUrl0)
						if orm.Error!=nil{
							fmt.Println(orm.Error)
						}
					}


				}
			}


			path1, err := common.BuildPathFile(poiId, 1)
			oss_path1:=fmt.Sprintf("test/meituan_licence/%d/%d.jpg",poiId,1);
			ossUrl1:=fmt.Sprintf("https://img2.lepucdn.cn/test/meituan_licence/%d/%d.jpg@!original.jpg",poiId,1);

			if err != nil || len(RestaurantLicenceImgUrl) < 1 {
				fmt.Printf("poiId=%d,err=%s\n", poiId,err)
			} else {
				if !common.Exists(path1) {
					body, err := fetcher.FetchGet(RestaurantLicenceImgUrl, header, "")
					if err != nil {
						fmt.Printf("poiId=%d,fecth_err=%s\n", poiId,err)
					} else {
						out, err := os.Create(path1)
						if err != nil {
							log.Printf("poiId=%d,Create_err=%s\n", poiId,err)
						} else {
							wlen, err := io.Copy(out, bytes.NewReader(body))
							if err != nil {
								log.Printf("poiId=%d,copy_err=%s\n", poiId,err)
							} else {
								fmt.Printf("poiId=%d,writeSuccess===========%d\n", poiId, wlen)
							}
						}
					}
				}else{
					err=itemSaver.OssUpload(path1,oss_path1);
					if err!=nil{
						fmt.Println(err)
					}else{
						orm.Model(&meituanMeishiPc.TableMeiTuanLicense{}).Where("poiId=?",poiId).
							Update("ossRestaurantLicenceImgUrl", ossUrl1);

						if orm.Error!=nil{
							fmt.Println(orm.Error)
						}
					}

				}
			}

		}

	}



}
